namespace Angelus.Oracle.Entity.OracleClient
{
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.Common;
    using System.Data.Metadata.Edm;
    using System.Globalization;
    using System.Runtime.Remoting;

    /// <summary>
    /// 
    /// </summary>
    public class DbOracleDataReader : DbDataReader
    {
        #region Fields

        private readonly PrimitiveType[] _columnTypes;
        private readonly DbDataReader _reader;

        #endregion

        #region Constructors

        /// <summary>
        /// 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="columnTypes"></param>
        internal DbOracleDataReader(DbDataReader reader, PrimitiveType[] columnTypes)
        {
            this._reader = reader;
            this._columnTypes = columnTypes;
        }

        #endregion

        #region Dispose Overrides

        /// <summary>
        /// 
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            GC.SuppressFinalize(this);
            if (disposing)
            {
                this._reader.Dispose();
            }

            base.Dispose(disposing);
        }

        #endregion

        #region Properties

        /// <summary>
        /// 
        /// </summary>
        public override int Depth
        {
            get { return this._reader.Depth; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override int FieldCount
        {
            get { return this._reader.FieldCount; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool HasRows
        {
            get { return this._reader.HasRows; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override bool IsClosed
        {
            get { return this._reader.IsClosed; }
        }

        public override int RecordsAffected
        {
            get { return this._reader.RecordsAffected; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override object this[string name]
        {
            get { return this.GetValue(this.GetOrdinal(name)); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override object this[int ordinal]
        {
            get { return this.GetValue(ordinal); }
        }

        #endregion

        #region DbDataReader Overrides

        /// <summary>
        /// 
        /// </summary>
        public override void Close()
        {
            GC.SuppressFinalize(this);
            this._reader.Close();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="requestedType"></param>
        /// <returns></returns>
        public override ObjRef CreateObjRef(Type requestedType)
        {
            return this._reader.CreateObjRef(requestedType);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            return this._reader.Equals(((DbOracleDataReader) obj)._reader);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override bool GetBoolean(int ordinal)
        {
            if (DBNull.Value == this.GetValue(ordinal))
            {
                return false;
            }

            return Convert.ToBoolean(this.GetValue(ordinal));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override byte GetByte(int ordinal)
        {
            return (byte) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <param name="dataOffset"></param>
        /// <param name="buffer"></param>
        /// <param name="bufferOffset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
        {
            return this._reader.GetBytes(ordinal, dataOffset, buffer, bufferOffset, length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override char GetChar(int ordinal)
        {
            return (char) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <param name="dataOffset"></param>
        /// <param name="buffer"></param>
        /// <param name="bufferOffset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public override long GetChars(int ordinal, long dataOffset, char[] buffer, int bufferOffset, int length)
        {
            return this._reader.GetChars(ordinal, dataOffset, buffer, bufferOffset, length);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override string GetDataTypeName(int ordinal)
        {
            if (null != this._columnTypes)
            {
                return this._columnTypes[ordinal].Name;
            }

            return this._reader.GetDataTypeName(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override DateTime GetDateTime(int ordinal)
        {
            return (DateTime) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override decimal GetDecimal(int ordinal)
        {
            return (decimal) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override double GetDouble(int ordinal)
        {
            return (double) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override IEnumerator GetEnumerator()
        {
            return this._reader.GetEnumerator();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override Type GetFieldType(int ordinal)
        {
            if (null != this._columnTypes)
            {
                return this._columnTypes[ordinal].ClrEquivalentType;
            }

            return this._reader.GetFieldType(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override float GetFloat(int ordinal)
        {
            return (float) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override Guid GetGuid(int ordinal)
        {
            return (Guid) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override short GetInt16(int ordinal)
        {
            return (short) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int GetHashCode()
        {
            return this._reader.GetHashCode();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override int GetInt32(int ordinal)
        {
            return (int) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override long GetInt64(int ordinal)
        {
            return (long) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override string GetName(int ordinal)
        {
            return this._reader.GetName(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public override int GetOrdinal(string name)
        {
            return this._reader.GetOrdinal(name);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override DataTable GetSchemaTable()
        {
            return this._reader.GetSchemaTable();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override string GetString(int ordinal)
        {
            return (string) this.GetValue(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override object GetValue(int ordinal)
        {
            object value = this._reader.GetValue(ordinal);
            if (null != this._columnTypes)
            {
                if ((!(value is DBNull)) && (value.GetType() != this._columnTypes[ordinal].ClrEquivalentType))
                {
                    value = ChangeType(value, this._columnTypes[ordinal].ClrEquivalentType);
                }
            }

            return value;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public override int GetValues(object[] values)
        {
            for (int index = 0, max = values.Length; index < max; index++)
            {
                values[index] = this.GetValue(index);
            }

            return values.Length;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns></returns>
        public override bool IsDBNull(int ordinal)
        {
            return this._reader.IsDBNull(ordinal);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override bool NextResult()
        {
            return this._reader.NextResult();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override bool Read()
        {
            return this._reader.Read();
        }

        #endregion

        #region Private Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private static object ChangeType(object source, Type type)
        {
            byte[] array = source as byte[];
            if ((null != array) && (typeof(Guid) == type))
            {
                return new Guid(array);
            }

            if ((source is DateTime) && (typeof (DateTimeOffset) == type))
            {
                return new DateTimeOffset((DateTime) source);
            }

            return Convert.ChangeType(source, type, CultureInfo.InvariantCulture);
        }

        #endregion
    }
}